package com.ruoyi.pay.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pay.domain.PayCoinCash;
import com.ruoyi.pay.domain.vo.PayCoinUserVO;
import com.ruoyi.pay.mapper.PayCoinCashMapper;
import com.ruoyi.pay.service.IPayCoinCashService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wx.domain.WxMaForm;
import com.ruoyi.wx.domain.WxPayReturnInfo;
import com.ruoyi.wx.service.WxMaPayService;

/**
 * 提现明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-26
 */
@Service
public class PayCoinCashServiceImpl extends ServiceImpl<PayCoinCashMapper, PayCoinCash> implements IPayCoinCashService {
    
	@Autowired
    private PayCoinCashMapper payCoinCashMapper;
	
	@Autowired
    private WxMaPayService wxMaPayService;
	
	@Autowired
	private ISysUserService sysUserService;
	
    /**
     * 查询提现明细
     * 
     * @param id 提现明细主键
     * @return 提现明细
     */
    @Override
    public PayCoinCash selectPayCoinCashById(String id)
    {
        return payCoinCashMapper.selectPayCoinCashById(id);
    }

    /**
     * 查询提现明细列表
     * 
     * @param payCoinCash 提现明细
     * @return 提现明细
     */
    @Override
    public List<PayCoinCash> selectPayCoinCashList(PayCoinCash payCoinCash)
    {
        return payCoinCashMapper.selectPayCoinCashList(payCoinCash);
    }

    /**
     * 修改提现明细
     * 
     * @param payCoinCash 提现明细
     * @return 结果
     */
    @Override
    public int updatePayCoinCash(PayCoinCash payCoinCash)
    {
        payCoinCash.setUpdateTime(DateUtils.getNowDate());
        return payCoinCashMapper.updatePayCoinCash(payCoinCash);
    }

    /**
     * 批量删除提现明细
     * 
     * @param ids 需要删除的提现明细主键
     * @return 结果
     */
    @Override
    public int deletePayCoinCashByIds(String[] ids)
    {
        return payCoinCashMapper.deletePayCoinCashByIds(ids);
    }

    /**
     * 删除提现明细信息
     * 
     * @param id 提现明细主键
     * @return 结果
     */
    @Override
    public int deletePayCoinCashById(String id)
    {
        return payCoinCashMapper.deletePayCoinCashById(id);
    }

	@Override
	public Long getCashByUserId(Long userId) {
		Long data = payCoinCashMapper.getCashByUserId(userId);
		if(data == null){
			data = 0L;
		}
		return data;
	}

	@Override
	public TableDataInfo getPageList(PayCoinCash payCoinCash) {
		payCoinCash.setUserId(SecurityUtils.getUserId());
		IPage<PayCoinCash> page = baseMapper.selectPageList(Condition.getPage(payCoinCash), payCoinCash);
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public WxPayReturnInfo addHonb(PayCoinCash payCoinCash) {
		WxPayReturnInfo info = new WxPayReturnInfo();
		SysUser user = sysUserService.getById(payCoinCash.getUserId());
		Integer totalFee = BigDecimal.valueOf(payCoinCash.getCoinNum()).multiply(user.getCashRate()).intValue();
		WxMaForm wxMaForm = new WxMaForm();
		wxMaForm.setAppId(user.getAppId());
		wxMaForm.setOpenId(user.getWxOpenId());
		wxMaForm.setTradeNo(payCoinCash.getTradeNo());
		wxMaForm.setTotalFee(totalFee);
		WxPaySendRedpackResult result;
		try {
			result = wxMaPayService.sendRedpack(wxMaForm, "邀请好友奖励");
			if("FAIL".equals(result.getResultCode())){
				// 发放失败
				if(payCoinCash.getId() != null){
					removeById(payCoinCash.getId());
				}
				info.setIsSuccess(false);
			}
		} catch (Exception e) {
			// 发放失败
			if(payCoinCash.getId() != null){
				removeById(payCoinCash.getId());
			}
			info.setIsSuccess(false);
		}
		return info;
	}

	@Override
	public WxPayReturnInfo addMoney(PayCoinCash payCoinCash, String appName) {
		WxPayReturnInfo info = new WxPayReturnInfo();
		info.setIsSuccess(true);
		SysUser user = sysUserService.getById(payCoinCash.getUserId());
		Integer totalFee = payCoinCash.getCoinNum().intValue();
		WxMaForm wxMaForm = new WxMaForm();
		wxMaForm.setAppId(user.getAppId());
		wxMaForm.setOpenId(user.getWxOpenId());
		wxMaForm.setTradeNo(payCoinCash.getTradeNo());
		wxMaForm.setTotalFee(totalFee);
		WxPaySendRedpackResult result;
		try {
			result = wxMaPayService.sendRedpack(wxMaForm, appName);
			if("FAIL".equals(result.getResultCode())){
				// 发放失败
				if(payCoinCash.getId() != null){
					removeById(payCoinCash.getId());
				}
				info.setIsSuccess(false);
			}
		} catch (Exception e) {
			// 发放失败
			if(payCoinCash.getId() != null){
				removeById(payCoinCash.getId());
			}
			info.setIsSuccess(false);
		}
		return info;
	}

	@Override
	public TableDataInfo getCashPageList(PayCoinCash payCoinCash) {
		IPage<PayCoinCash> page = baseMapper.selectCashPageList(Condition.getPage(payCoinCash), payCoinCash);
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public TableDataInfo getMyCoinCashList(PayCoinUserVO payCoinUser) {
		IPage<PayCoinUserVO> page = baseMapper.getMyCoinCashList(Condition.getPage(payCoinUser), payCoinUser);
		for(PayCoinUserVO cu : page.getRecords()){
			Integer coinNum = cu.getCoinNum();
			cu.setCoinNum(coinNum/100);
			cu.setCoinMoney(BigDecimal.valueOf(coinNum).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP));
		}
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public Integer getMyCashMoney(PayCoinUserVO payCoinUser) {
		Integer cash = baseMapper.getMyCashMoney(payCoinUser);
		if(cash == null){
			cash = 0;
		}
		return cash;
	}
	
}
